package eclipse.lib.graphics.primitives;
import com.sun.opengl.util.texture.Texture;
import eclipse.Constants;
/**
 * The GLSprite class is an extension of the GLQuad class. This encompases all of the functions that are required for drawing a sprite to the screen.
 */
public class GLSprite extends GLQuad
{
	private float width, height;
	private float u1, u2, v1, v2;
	private float ratioW, ratioH;
	private float verts[] = new float[12];
	private int frames;
	/**
	 * Sets the texture of the sprite. Requires number of frames in the sprite.	
	 * @param texture Texture to use for the sprite
	 * @param frames Frames across to pull from the texture
	 */
	public void setTexture(Texture texture, int frames)
	{
		GLPolygon polygon = getPolygon();
		polygon.setTexture(texture);
		setFrames(frames);
		width 	= texture.getWidth()  / frames;
		height 	= texture.getHeight() / 4;
		ratioW = width / 32f;
		ratioH = height / 32f;
		verts[3] = Constants.TILERECT[3] * ratioW;
		verts[6] = Constants.TILERECT[6] * ratioW;
		verts[7] = Constants.TILERECT[7] * ratioH;
		verts[10]= Constants.TILERECT[10]* ratioH;
		setQuad(verts);
	}
	/**
	 * Sets the image of the sprite from a sub-image of the sprite sheet.
	 * @param xSource The left-most coordinate of the pixel of the sprite image
	 * @param ySource The top-most coordinate of the pixel of the sprite image
	 */
	public void setSprite(int xSource, int ySource)
	{
		GLPolygon polygon = getPolygon();
		u1 = (xSource * width) / polygon.getTexture().getWidth();
		v1 = (ySource * height) / polygon.getTexture().getHeight();
		u2 = (xSource * width + width) / polygon.getTexture().getWidth();
		v2 = (ySource * height + height) / polygon.getTexture().getHeight();
		polygon.setUV(0, u1, v1);
		polygon.setUV(1, u2, v1);
		polygon.setUV(2, u2, v2);
		polygon.setUV(3, u1, v2);
		
	}	
	/**
	 * Sets the number of frames in the sprite sheet
	 * @param frames Frames across in the sprite sheet
	 */
	public void setFrames(int frames)
	{
		this.frames = frames;
	}
	/**
	 * Returns the number of frames in the sprite sheet
	 * @return Frames across that exist in the sprite sheet
	 */
	public int getFrames()
	{
		return frames;
	}
}
